package com.company.psm.serviceimpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.company.psm.entity.Category;
import com.company.psm.entity.Store;
import com.company.psm.genericdao.CustomGenericDAO;
import com.company.psm.service.CategoryManagement;

@Service(value="categoryManagement")
@Transactional(readOnly = true, propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
public class CategoryManagementImpl extends CustomGenericDAO<Object> implements CategoryManagement{

	public Object find(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	public Object[] find(Long... ids) {
		// TODO Auto-generated method stub
		return null;
	}

	public Object getReference(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	public Object[] getReferences(Long... ids) {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean removeById(Long id) {
		// TODO Auto-generated method stub
		return false;
	}

	public void removeByIds(Long... ids) {
		// TODO Auto-generated method stub
		
	}

	public List<Category> getAll() {
		// TODO Auto-generated method stub
		return _all(Category.class);
	}

	public List<Category> getAllByStore(Long storeId) {
		List<Category> categories = _all(Category.class);
		List<Category> storeCategories = new ArrayList<Category>();
		for (Category category : categories) {
			if(category.getStore().getId() == storeId || category.getStore().getId() == 3){
				storeCategories.add(category);
			}
		}
		return storeCategories;
	}
	@Transactional(readOnly = false)
	public void careateCategory(String name, String description, Store store) {
		Category category = new Category();
		category.setName(name);
		category.setStore(store);
		category.setDescription(description);
		_save(category);
	}
	@Transactional(readOnly = false)
	public void updateCategory(String id, String name, String description,
			long storeId) {
		Category category = _get(Category.class, Long.parseLong(id));
		category.setName(name);
		category.setDescription(description);
		_update(category);
	}
	@Transactional(readOnly = false)
	public void delete(String id) {
		_deleteById(Category.class, id);
	}

	public Category getById(String id) {
		// TODO Auto-generated method stub
		return _get(Category.class, Long.parseLong(id));
	}

}
